package com.dzm.content.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzm.anno.RequirePermission;
import com.dzm.constants.RabbitConstant;
import com.dzm.content.entity.Content;
import com.dzm.content.service.ContentService;
import com.dzm.content.vo.SearchVo;
import com.dzm.utils.R;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：Dzm
 * 时间：2024-09-13-16:49
 * 功能：
 */

@RestController
@RequestMapping("/api/content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequirePermission("content:search")
    @PostMapping("/search/{pageNum}/{pageSize}")
    public R search(@PathVariable("pageNum") Integer pageNum,
                    @PathVariable("pageSize")Integer pageSize,
                    @RequestBody SearchVo vo) {
        Page<Content> pageModel = contentService.search(pageNum, pageSize, vo);

        return R.ok().data("pageModel", pageModel);
    }

    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id")Integer id) {
        Content byId = contentService.getById(id);
        return R.ok().data("content", byId);
    }

    @PostMapping("/save")
    public R save(@RequestBody Content content) {
        contentService.save(content);
        rabbitTemplate.convertAndSend(RabbitConstant.CONTENT_EXC, RabbitConstant.CONTENT_EXC_SAVE, content);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody Content content) {
        contentService.updateById(content);
        rabbitTemplate.convertAndSend(RabbitConstant.CONTENT_EXC, RabbitConstant.CONTENT_EXC_SAVE, content);
        return R.ok();
    }

    @RequirePermission("content:remove")
    @DeleteMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        contentService.removeBatchByIds(ids);
        rabbitTemplate.convertAndSend(RabbitConstant.CONTENT_EXC, RabbitConstant.CONTENT_SAVE_REMOVE, ids);
        return R.ok();
    }
}
